package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.support.v7.widget.Toolbar;
import android.view.View;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.hanks.htextview.evaporate.EvaporateTextView;
import com.hanks.htextview.fade.FadeTextView;
import com.hanks.htextview.fall.FallTextView;
import com.hanks.htextview.line.LineTextView;
import com.hanks.htextview.rainbow.RainbowTextView;
import com.hanks.htextview.scale.ScaleTextView;
import com.hanks.htextview.typer.TyperTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by TCL on 2019/6/21.
 * Describe:
 */
public class HTextActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.linetextview)
    LineTextView linetextview;
    @BindView(R.id.fadetextview)
    FadeTextView fadetextview;
    @BindView(R.id.typertextview)
    TyperTextView typertextview;
    @BindView(R.id.rainbowtextview)
    RainbowTextView rainbowtextview;
    @BindView(R.id.scaletextview)
    ScaleTextView scaletextview;
    @BindView(R.id.evaporatetextview)
    EvaporateTextView evaporatetextview;
    @BindView(R.id.falltextview)
    FallTextView falltextview;

    private List<String> mList;
    private Observable<String> data;
    private Observable<Long> time;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_htext;
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mList.add("My heart is with you");
        mList.add("Sow nothing， reap nothing");
        mList.add("Sharp tools make good work");
        mList.add("Time tries truth");
        mList.add("Life is but a span");

        data = Observable.fromIterable(mList);
        time = Observable.interval(2, TimeUnit.SECONDS);
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_htext));
    }


    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                showTextByType(1);
                break;

            case R.id.btn2:
                showTextByType(2);
                break;

            case R.id.btn3:
                showTextByType(3);
                break;

            case R.id.btn4:
                showTextByType(4);
                break;

            case R.id.btn5:
                showTextByType(5);
                break;

            case R.id.btn6:
                showTextByType(6);
                break;

            case R.id.btn7:
                showTextByType(7);
                break;
        }
    }

    private void showTextByType(int type) {
        Observable.zip(data, time, new BiFunction<String, Long, Object>() {
            @Override
            public Object apply(String s, Long aLong) throws Exception {
                return s;
            }

        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onNext(Object o) {
                        String text = o.toString();
                        switch (type) {
                            case 1:
                            default:
                                linetextview.animateText(text);
                                break;

                            case 2:
                                fadetextview.animateText(text);
                                break;

                            case 3:
                                typertextview.animateText(text);
                                break;

                            case 4:
                                rainbowtextview.animateText(text);
                                break;

                            case 5:
                                scaletextview.animateText(text);
                                break;

                            case 6:
                                evaporatetextview.animateText(text);
                                break;

                            case 7:
                                falltextview.animateText(text);
                                break;

                        }
                        System.out.println("输出了;" + o.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        System.out.println("输出了错误;" + e.getMessage());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
